﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.IO;

namespace GraphTheory.Task1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Matrix.Text = "2\n0 1\n1 0";
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Graph counter = new Graph(Matrix.Text, GraphType.UndirectedGraphFromString);
            System.Windows.MessageBox.Show("Компонент связности: " + counter.CalculateNumberOfConnectedComponents().ToString(), "Количество компонент связности");
        }

        private void TextBox_TextChanged_1(object sender, TextChangedEventArgs e)
        {

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Graph graph = new Graph(Matrix.Text, GraphType.UndirectedGraphFromString);
            System.Windows.MessageBox.Show(graph.IsEuler() == 0 ? "Эйлеров" : "Не Эйлеров, обход остановлен на вершине " + graph.IsEuler().ToString(), "Эйлеровость");
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            if (System.Windows.Forms.DialogResult.OK == dialog.ShowDialog())
            {
                StreamReader reader = new StreamReader(dialog.OpenFile());
                Matrix.Text = reader.ReadToEnd();
            }
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            Graph graph = new Graph(Matrix.Text, GraphType.UndirectedGraphFromString);
            try
            {
                System.Windows.Forms.MessageBox.Show(graph.IsChordal() ? "Граф скорее всего ходральный, так как есть смежно-поглощающая вершина" : "Граф - не хордальный", "Хордальность");
            }
            catch (NotImplementedException ex)
            {
                System.Windows.MessageBox.Show("Not yet implemented");
            }
        }

        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            Graph graph = new Graph(Matrix.Text, GraphType.UndirectedGraphFromString);
            try
            {
                System.Windows.Forms.MessageBox.Show("Независимое множество: " + graph.GetIndependentSet());
            }
            catch (NotImplementedException)
            {
                System.Windows.Forms.MessageBox.Show("Not yet implemented");
            }
        }

        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            Graph graph = new Graph(Matrix.Text, GraphType.UndirectedGraphFromString);
            try
            {
                System.Windows.Forms.MessageBox.Show("Раскраска: " + graph.GetColoring());
            }
            catch (NotImplementedException)
            {
                System.Windows.Forms.MessageBox.Show("Not yet inmplemented");
            }

        }

        private void Button_Click_6(object sender, RoutedEventArgs e)
        {
            Graph graph = new Graph(Matrix.Text, GraphType.DirectedGraphFromString);
            try
            {
                System.Windows.Forms.MessageBox.Show("Кратчайшие пути до вершин графа" + graph.BellmanFord() + "\n Если необходимо получит и сами пути, сообщите разработчику");
            }
            catch (NotImplementedException)
            {
                System.Windows.Forms.MessageBox.Show("Not yet implemented");
            }
        }

        private void Button_Click_7(object sender, RoutedEventArgs e)
        {
            Graph graph = new Graph(Matrix.Text, GraphType.DirectedGraphFromString);
            try
            {
                System.Windows.Forms.MessageBox.Show("Кратчайшие пути между вершинами:\n" + graph.FloydWorshell());
            }
            catch (NotImplementedException)
            {
                System.Windows.Forms.MessageBox.Show("Not yet implemented");
            }
        }

        private void Button_Click_8(object sender, RoutedEventArgs e)
        {
            try
            {
                String input = Matrix.Text;
                String graphDef = input.Split('#')[0];
                String inputDef = input.Split('#')[1];
                StringScanner scanner = new StringScanner(inputDef);
                int sourceNode = scanner.nextInt();
                int sinkNode = scanner.nextInt();
                WeightedDigraph graph = WeightedDigraph.ParseFromString(graphDef);
                int[,] flows = graph.GetMaxFlows(sourceNode, sinkNode);

                string output = string.Empty;
                for (int i = 0; i < graph.Vertices.Length; i++)
                {
                    for (int j = 0; j < graph.Vertices.Length; j++)
                    {
                        output += flows[i, j].ToString() + " ";
                    }
                    output += "\n";
                }

                int maxFlow = 0;
                foreach (var vrt in WeightedDigraph.GetIngoingVertices(graph, sinkNode))
                {
                    maxFlow += flows[vrt, sinkNode];
                }
                output += "Максимальный поток: " + maxFlow.ToString();
                System.Windows.Forms.MessageBox.Show(output);
            }
            catch (NotImplementedException ex)
            {
                System.Windows.Forms.MessageBox.Show("Not yet implemented");
            }
        }

        private void Button_Click_9(object sender, RoutedEventArgs e)
        {
            try
            {
                Graph graph = new Graph(Matrix.Text, GraphType.DirectedGraphFromString);
                System.Windows.Forms.MessageBox.Show("Типы дуг:\n" + graph.GetTypes());
            }
            catch (Exception)
            {
                System.Windows.Forms.MessageBox.Show("Not yet");
            }
        }

        private void Button_Click_10(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.MessageBox.Show("Для работы с неориентированными графами: \n Кол-во вершин \n Матрица смежности \n" +
                "Для работы с ориентированными графами: \n Кол-во вершин \n матрица весов (отсутствие дуги - _ )\n" +
                "Для работы с графами с пропускными способностями: \n Кол-во вершин \n Матрица смежности \n матрица пропусных способностей \n # \n номера исхода и стока"
                );
        }

        private void Button_Click_11(object sender, RoutedEventArgs e)
        {
            String graphDef = Matrix.Text;
            Digraph digraph = Digraph.ParseFromString(graphDef);
            List<int>[] Components = digraph.GetStronglyConnectedComponents();

            int i = 1;
            string output = string.Empty;
            foreach (var component in Components)
            {
                output += "Компонента " + i + ":";
                foreach (var vertex in component)
                {
                    output += " " + vertex;
                }
                i++;
            }
            System.Windows.Forms.MessageBox.Show(output);
        }

        private void Button_Click_12(object sender, RoutedEventArgs e)
        {
            String graphDef = Matrix.Text;
            GraphForPrim graph = GraphForPrim.ParseFromString(graphDef);
            System.Windows.Forms.MessageBox.Show(graph.GetMinimumSpanningTree().ToString());
        }
    }
}
